package com.compass.doctor.ui.patient.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import com.compass.doctor.ui.patient.modle.CircleModel
import com.compass.framework.ext.toJson
import com.netease.nimlib.sdk.NIMClient
import com.netease.nimlib.sdk.msg.MsgService
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum
import com.netease.nimlib.sdk.msg.model.CustomNotification
import com.netease.nimlib.sdk.msg.model.CustomNotificationConfig
import org.json.JSONObject

/**
 * Description： 画线
 * Date:2024/10/29 14:22
 */
class CircleView : View {
    var circles: MutableList<CircleModel> = ArrayList()
    var circlesLine: MutableList<CircleModel> = ArrayList()

    private var isLine: Boolean = false //是否是画线
    private var doctorImId: String = "1815788826166136174"
    private var width: Int = 0
    private var height: Int = 0
    private var mPath: Path = Path()


    constructor(context: Context?) : super(context)

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes)

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val paint = Paint()
        val paintLine = Paint()


        for (circle in circles) {
            circle.drawSelf(canvas, paint)
        }

        drawLine(canvas, paintLine, mPath)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        val action = event.action
        val circle: CircleModel
        val x = event.x
        val y = event.y
        when (action) {
            MotionEvent.ACTION_DOWN -> {
                //实例化圆
                circle = CircleModel(x, y)
                if (isLine) {
                    circlesLine.clear()
                    mPath.moveTo(x, y)
                    circlesLine.add(circle)
                } else {
                    circles.add(circle)
                    sendXY(x, y)
                }
            }

            MotionEvent.ACTION_UP -> if (isLine) {
                if (circlesLine.size > 0) sendLine(circlesLine)
            }

            MotionEvent.ACTION_MOVE -> if (isLine) {
                circlesLine.add(CircleModel(x, y))
                mPath.lineTo(x, y)
            }
        }
        //记录当前触摸点得当前得坐标
        invalidate()
        return true
    }

    /**
     * 画线
     *
     * @param canvas
     * @param paint
     * @param path
     */
    fun drawLine(canvas: Canvas, paint: Paint, path: Path?) {
        paint.isAntiAlias = true
        paint.style = Paint.Style.STROKE
        paint.strokeCap = Paint.Cap.ROUND
        paint.strokeWidth = 6f
        paint.color = Color.BLUE
        canvas.drawPath(path!!, paint)
    }


    fun setDoctorImId(doctorImId: String) {
        this.doctorImId = doctorImId
    }

    fun setIsLIne(isLine: Boolean) {
        this.isLine = isLine
    }

    /**
     * 清空标注
     */
    fun clearCircles() {
        circles.clear()
        circlesLine.clear()
        mPath.reset()
        invalidate()
        removeXY()
    }

    /**
     * 当前屏幕的宽高
     *
     * @param width
     * @param height
     */
    fun setWidthAndHeight(width: Int, height: Int) {
        this.width = width
        this.height = height
        Log.e("width", "==$width")
        Log.e("height", "==$height")
    }


    private fun sendXY(x: Float, y: Float) {
        //构造自定义通知，指定接收者
        val notification = CustomNotification()
        notification.sessionId = doctorImId
        notification.sessionType = SessionTypeEnum.P2P
        // 构建通知的具体内容。为了可扩展性，这里采用 json 格式，以 "id" 作为类型区分。
        val json = JSONObject()
        json.put("id", "add")
        json.put("x", x)
        json.put("y", y)
        json.put("width", width)
        json.put("height", height)
        notification.content = json.toString()
        // 若接收者不在线，则当其再次上线时，将收到该自定义系统通知。若设置为 true，则再次上线时，将收不到该通知。
        notification.isSendToOnlineUserOnly = true
        NIMClient.getService(MsgService::class.java).sendCustomNotification(notification)

    }


    private fun sendLine(lines: List<CircleModel>) {
        Log.e("发送线数据==", lines.toJson());
        //构造自定义通知，指定接收者
        val notification = CustomNotification()
        notification.sessionId = doctorImId
        notification.sessionType = SessionTypeEnum.P2P
        // 构建通知的具体内容。为了可扩展性，这里采用 json 格式，以 "id" 作为类型区分。
        val json = JSONObject()
        json.put("id", "addLine")
        json.put("lines", lines.toJson())
        json.put("width", width)
        json.put("height", height)
        notification.content = json.toString()
        // 若接收者不在线，则当其再次上线时，将收到该自定义系统通知。若设置为 true，则再次上线时，将收不到该通知。
        notification.isSendToOnlineUserOnly = true
        NIMClient.getService(MsgService::class.java).sendCustomNotification(notification)
    }

    private fun removeXY() {

        //构造自定义通知，指定接收者
        val notification = CustomNotification()
        notification.sessionId = doctorImId
        notification.sessionType = SessionTypeEnum.P2P
        // 构建通知的具体内容。为了可扩展性，这里采用 json 格式，以 "id" 作为类型区分。
        val json = JSONObject()
        json.put("id", "remove")
        // 若接收者不在线，则当其再次上线时，将收到该自定义系统通知。若设置为 true，则再次上线时，将收不到该通知。
        notification.isSendToOnlineUserOnly = true
        NIMClient.getService(MsgService::class.java).sendCustomNotification(notification)

    }
}
